package com.our.sys.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.service.HzService;
import com.our.sys.project1.dao.BusinesslicenceDao;
import com.our.sys.project1.dao.MedicallicensesDao;
import com.our.sys.project1.dao.ProduclicensesDao;
import com.our.sys.project1.entity.Businesslicence;
import com.our.sys.project1.entity.Medicallicenses;
import com.our.sys.project1.entity.Produclicenses;

@Service
@Transactional
public class LicensesService {

    @Autowired
    private MedicallicensesDao medicallicensesDao;
    @Autowired
    private ProduclicensesDao produclicensesDao;
    @Autowired
    private BusinesslicenceDao businesslicenceDao;

    @Autowired
    private HzService hzService;

    private static Logger logger = LoggerFactory.getLogger(LicensesService.class);

    @Transactional(readOnly = true)
    public Page<Medicallicenses> queryM(Page<Medicallicenses> page, List<PropertyFilter> filters) {
        Page<Medicallicenses> curPage = medicallicensesDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public Page<Produclicenses> queryP(Page<Produclicenses> page, List<PropertyFilter> filters) {
        Page<Produclicenses> curPage = produclicensesDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public Page<Businesslicence> queryB(Page<Businesslicence> page, List<PropertyFilter> filters) {
        Page<Businesslicence> curPage = businesslicenceDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public Medicallicenses findMById(Long id) {
        return medicallicensesDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    public Produclicenses findPById(Long id) {
        return produclicensesDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    public Businesslicence findBById(Long id) {
        return businesslicenceDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    public Medicallicenses findMBySupplierId(Long id) {
        return medicallicensesDao.findUnique("from Medicallicenses where supplierId = ?", id);
    }

    @Transactional(readOnly = true)
    public Produclicenses findPBySupplierId(Long id) {
        return produclicensesDao.findUnique("from Produclicenses where supplierId = ?", id);
    }

    @Transactional(readOnly = true)
    public Businesslicence findBBySupplierId(Long id) {
        return businesslicenceDao.findUnique("from Businesslicence where supplierId = ?", id);
    }

    @Transactional(readOnly = true)
    public Long countIsIdM(Long supplierId) {
        Long result = medicallicensesDao.findUnique("select count(a) from Medicallicenses a where a.supplierId = ?",
                supplierId);
        if (null == result) {
            result = 0L;
        }
        return result;
    }

    @Transactional(readOnly = true)
    public Long countIsIdP(Long supplierId) {
        Long result = produclicensesDao.findUnique("select count(a) from Produclicenses a where a.supplierId = ?",
                supplierId);
        if (null == result) {
            result = 0L;
        }
        return result;
    }

    @Transactional(readOnly = true)
    public Long countIsIdB(Long supplierId) {
        Long result = businesslicenceDao.findUnique("select count(a) from Businesslicence a where a.supplierId = ?",
                supplierId);
        if (null == result) {
            result = 0L;
        }
        return result;
    }

    public void saveM(Medicallicenses medicallicenses) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(medicallicenses.getSupplier());
            medicallicenses.setPy(map.get("py"));
            medicallicenses.setWb(map.get("wb"));

            Map<String, String> map2 = hzService.getMemo(medicallicenses.getHighValueAllName());
            medicallicenses.setHapy(map2.get("py"));
            medicallicenses.setHawb(map2.get("wb"));

            Map<String, String> map3 = hzService.getMemo(medicallicenses.getHighValueItemName());
            medicallicenses.setHipy(map3.get("py"));
            medicallicenses.setHiwb(map3.get("wb"));

            if (null != medicallicenses.getId()) {
                Medicallicenses getMedicallicenses = this.findMById(medicallicenses.getId());

                if (this.countIsIdM(getMedicallicenses.getSupplierId()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个医疗器材许可证！");
                }

                getMedicallicenses.setSupplierId(medicallicenses.getSupplierId());
                getMedicallicenses.setSupplier(medicallicenses.getSupplier());
                getMedicallicenses.setPy(medicallicenses.getPy());
                getMedicallicenses.setWb(medicallicenses.getWb());
                getMedicallicenses.setHapy(medicallicenses.getHapy());
                getMedicallicenses.setHawb(medicallicenses.getHawb());
                getMedicallicenses.setHipy(medicallicenses.getHipy());
                getMedicallicenses.setHiwb(medicallicenses.getHiwb());
                getMedicallicenses.setHighValueAllName(medicallicenses.getHighValueAllName());
                getMedicallicenses.setHighValueItemName(medicallicenses.getHighValueItemName());
                getMedicallicenses.setShortName(medicallicenses.getShortName());
                getMedicallicenses.setProduceId(medicallicenses.getProduceId());
                getMedicallicenses.setProduce(medicallicenses.getProduce());
                getMedicallicenses.setRegisterNo(medicallicenses.getRegisterNo());
                getMedicallicenses.setShortName(medicallicenses.getShortName());
                getMedicallicenses.setCheckDate(medicallicenses.getCheckDate());
                getMedicallicenses.setEndDate(medicallicenses.getEndDate());
                medicallicensesDao.save(getMedicallicenses);
            } else {

                if (this.countIsIdM(medicallicenses.getSupplierId()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个医疗器材许可证！");
                }

                medicallicensesDao.save(medicallicenses);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void saveP(Produclicenses produclicenses) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(produclicenses.getSupplier());
            produclicenses.setPy(map.get("py"));
            produclicenses.setWb(map.get("wb"));

            if (null != produclicenses.getId()) {
                Produclicenses getProduclicenses = this.findPById(produclicenses.getId());

                if (this.countIsIdP(getProduclicenses.getSupplierId()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个生产经营许可证！");
                }

                getProduclicenses.setSupplierId(produclicenses.getSupplierId());
                getProduclicenses.setSupplier(produclicenses.getSupplier());
                getProduclicenses.setBusinessRange(produclicenses.getBusinessRange());
                getProduclicenses.setCompanyPerson(produclicenses.getCompanyPerson());
                getProduclicenses.setLicensDate(produclicenses.getLicensDate());
                getProduclicenses.setLicensesOrg(produclicenses.getLicensesOrg());
                getProduclicenses.setPy(produclicenses.getPy());
                getProduclicenses.setWb(produclicenses.getWb());
                getProduclicenses.setProduceId(produclicenses.getProduceId());
                getProduclicenses.setProduce(produclicenses.getProduce());
                getProduclicenses.setEndDate(produclicenses.getEndDate());
                getProduclicenses.setRegisterAddress(produclicenses.getRegisterAddress());
                getProduclicenses.setProduceNo(produclicenses.getProduceNo());
                getProduclicenses.setProduceAddress(produclicenses.getProduceAddress());
                produclicensesDao.save(getProduclicenses);
            } else {

                if (this.countIsIdM(produclicenses.getSupplierId()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个生产经营许可证！");
                }

                produclicensesDao.save(produclicenses);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void saveB(Businesslicence businesslicence) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(businesslicence.getSupplier());
            businesslicence.setPy(map.get("py"));
            businesslicence.setWb(map.get("wb"));

            if (null != businesslicence.getId()) {
                Businesslicence getBusinesslicence = this.findBById(businesslicence.getId());

                if (this.countIsIdP(getBusinesslicence.getSupplierId()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个营业执照！");
                }

                getBusinesslicence.setSupplierId(businesslicence.getSupplierId());
                getBusinesslicence.setSupplier(businesslicence.getSupplier());
                getBusinesslicence.setAddress(businesslicence.getAddress());
                getBusinesslicence.setBusinessLast(businesslicence.getBusinessLast());
                getBusinesslicence.setBusinessRange(businesslicence.getBusinessRange());
                getBusinesslicence.setCompanyType(businesslicence.getCompanyType());
                getBusinesslicence.setCopyNo(businesslicence.getCopyNo());
                getBusinesslicence.setPerson(businesslicence.getPerson());
                getBusinesslicence.setRegisterMoney(businesslicence.getRegisterMoney());
                getBusinesslicence.setRegisterNo(businesslicence.getRegisterNo());
                getBusinesslicence.setSetUpDate(businesslicence.getSetUpDate());
                getBusinesslicence.setYearCheck(businesslicence.getYearCheck());
                getBusinesslicence.setPy(businesslicence.getPy());
                getBusinesslicence.setWb(businesslicence.getWb());
                businesslicenceDao.save(getBusinesslicence);
            } else {

                if (this.countIsIdM(businesslicence.getSupplierId()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个营业执照！");
                }

                businesslicenceDao.save(businesslicence);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteB(Long id) throws Exception {
        try {
            businesslicenceDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteP(Long id) throws Exception {
        try {
            produclicensesDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteM(Long id) throws Exception {
        try {
            medicallicensesDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void saveImgSrcB(Long id, String filePath) throws Exception {
        try {
            Businesslicence b = businesslicenceDao.findOneBy("id", id);
            b.setImgSrc(filePath);
            businesslicenceDao.save(b);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void saveImgSrcP(Long id, String filePath) throws Exception {
        try {
            Produclicenses p = produclicensesDao.findOneBy("id", id);
            p.setImgSrc(filePath);
            produclicensesDao.save(p);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
    
    public void saveImgSrcM(Long id, String filePath) throws Exception {
        try {
            Medicallicenses m = medicallicensesDao.findOneBy("id", id);
            m.setImgSrc(filePath);
            medicallicensesDao.save(m);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

}
